/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.tarifador.view;

import br.com.tarifador.controller.TroncoRN;
import br.com.tarifador.dataModel.TroncoTableModel;
import br.com.tarifador.model.Tronco;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hibernate.HibernateException;
import org.primefaces.context.RequestContext;

/**
 *
 * @author charles
 */
@ManagedBean
@SessionScoped
public class TroncoBean {

    private TroncoRN troncoRN = new TroncoRN();
    private List<Tronco> listaTronco;
    private Tronco troncoSelected;
    private TroncoTableModel troncoModel;

    public void salvaTronco() {
        boolean salvar = false;
        try {
            salvar = troncoRN.salvaTronco();
        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        if (salvar == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaTronco();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));
        }
    }

    public void listaTronco() {
        listaTronco = new ArrayList<Tronco>();
        try {
            listaTronco = troncoRN.listaDeTroncos();

        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        this.troncoModel = new TroncoTableModel(listaTronco);
    }

    public void alteraTronco() {
        boolean valida = false;
        try {
            valida = troncoRN.updateTronco(troncoSelected);

        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        if (valida == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaTronco();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));

        }
    }

    public void deleteTronco() {
        boolean valida = false;
        try {
            valida = troncoRN.deletaTronco(troncoSelected.getId());
        }catch (NullPointerException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Selecione um campo!", ""));
        }catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados!\n " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados!\n " + e.getMessage(), ""));
        }
        if (valida == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaTronco();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));
        }
    }

    public void btnAlterarTronco() {
        RequestContext request = RequestContext.getCurrentInstance();
        try {
            if (troncoSelected.getId() != null && troncoSelected.getId() > 0) {
                request.addCallbackParam("sucesso", true);
            } else {
                request.addCallbackParam("sucesso", false);
            }
        } catch (NullPointerException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Selecione um campo!", ""));
        }
    }

    public void limpaLista() {
        if (troncoModel != null) {
            listaTronco.clear();
            troncoModel = new TroncoTableModel(listaTronco);
        }
    }

    public TroncoRN getTroncoRN() {
        return troncoRN;
    }

    public void setTroncoRN(TroncoRN troncoRN) {
        this.troncoRN = troncoRN;
    }

    public List<Tronco> getListaTronco() {
        return listaTronco;
    }

    public void setListaTronco(List<Tronco> listaTronco) {
        this.listaTronco = listaTronco;
    }

    public Tronco getTroncoSelected() {
        return troncoSelected;
    }

    public void setTroncoSelected(Tronco troncoSelected) {
        this.troncoSelected = troncoSelected;
    }

    public TroncoTableModel getTroncoModel() {
        return troncoModel;
    }

    public void setTroncoModel(TroncoTableModel troncoModel) {
        this.troncoModel = troncoModel;
    }

   
}
